package com.ruoyi.api.zero;

import com.ruoyi.project.projectInfo.domain.ProjectInfo;
import com.ruoyi.project.projectInfo.service.IProjectInfoService;
import com.ruoyi.redis.RedisService;
import com.ruoyi.system.cache.post.PostCachService;
import com.ruoyi.system.cache.user.UserCacheService;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.ISysPostService;
import com.ruoyi.system.service.ISysUserService;
import net.bytebuddy.asm.Advice;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/api/zeroUser")
@RestController
public class ZeroUserController {
  @Autowired
  private ISysUserService userService;
  @Autowired
  private UserCacheService userCacheService;
  @Autowired
  private RedisService redisService;

  @GetMapping("/userToRedis")
  public Object userToRedis() {
    List<SysUser> list = userService.selectUserList(new SysUser());
    for (SysUser sysUser : list) {
      userCacheService.addToCache(sysUser);
    }
    return 1;
  }

 /**
   *修复所有未绑定岗位的账户
   */
  @GetMapping("/accountPostFix")
  public void accountPostFix(){
    List<SysUser> list = userService.selectUserList(new SysUser());
    for(SysUser user:list){
      if(StringUtils.isEmpty(user.getUserCode())){
        SysUser sysUser = userService.selectUserById(user.getUserId());
        List<SysPost> posts = sysUser.getPosts();
        if(posts == null || posts.size() == 0){
            //部门id125，岗位id 4
          sysUser.setDeptId(125l);
          sysUser.setPostIds(new Long[]{4l});
          userService.updateUser(sysUser);
        }{
          sysUser.setPostIds(new Long[]{posts.get(0).getPostId()});
          userService.updateUser(sysUser);
          System.out.println("===accountPostFix===");
        }
      }
    }
  }


}
